package com.snack.system.service.impl;

import com.snack.common.core.utils.StringUtils;
import com.snack.common.core.utils.dif.DifUtils;
import com.snack.system.domain.TsResource;
import com.snack.system.mapper.TsResourceMapper;
import com.snack.system.service.ITsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 资源Service业务层处理
 * 
 * @author snack
 * @date 2022-09-18
 */
@Service
public class TsResourceServiceImpl implements ITsResourceService 
{
    @Autowired
    private TsResourceMapper resourceMapper;
    /**
     * 查询资源
     * 
     * @param resourceId 资源主键
     * @return 资源
     */
    @Override
    public TsResource selectById(Long resourceId)
    {
        return resourceMapper.selectById(resourceId);
    }

    /**
     * 查询资源列表
     * 
     * @param resource 资源
     * @return 资源
     */
    @Override
    public List<TsResource> selectList(TsResource resource)
    {
        return resourceMapper.selectList(resource);
    }

    /**
     * 通过pics查询数据
     * @param pics
     * @return
     */
    @Override
    public List<TsResource> selectListByPics(String pics){
        return resourceMapper.selectListByPics(pics);
    }

    /**
     * 新增资源
     * 
     * @param resource 资源
     * @return 结果
     */
    @Override
    public int insert(TsResource resource)
    {
        if(StringUtils.isNull(resource)) { return 0; }
        return resourceMapper.insert(resource);
    }

    /**
     * 批量插入
     * @param list
     * @return
     */
    @Override
    public int insertBatch(List<TsResource> list) {
        if(StringUtils.isEmpty(list)) { return 0; }
        return resourceMapper.insertBatch(list);
    }

    /**
     * 批量更改
     * @param list
     * @param pics
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateBatch(List<TsResource> list,String pics) {
        if(StringUtils.isEmpty(list)) {
            resourceMapper.deleteByPics(pics);
            return;
        }else{
            list.forEach((item)->item.setPics(pics));
        }
        List<TsResource> oldList = this.selectListByPics(pics);
        if (StringUtils.isEmpty(oldList)){
            this.insertBatch(list);
            return;
        }
        Map<Integer, List<TsResource>> listDiff = DifUtils.findListDiff(list, oldList);
        if(StringUtils.isNotEmpty(listDiff.get(0))){
            //新增
            this.insertBatch(listDiff.get(0));
        }
        if (StringUtils.isNotEmpty(listDiff.get(1))){
            //更改
        }
        if (StringUtils.isNotEmpty(listDiff.get(2))){
            //删除
            resourceMapper.deleteBatch(listDiff.get(2));
        }
    }

    /**
     * 修改资源
     * 
     * @param resource 资源
     * @return 结果
     */
    @Override
    public int update(TsResource resource)
    {
        return resourceMapper.update(resource);
    }

    /**
     * 修改资源
     *
     * @param resource 资源
     * @return 结果
     */
    @Override
    public void update(TsResource resource,String pics){
        if(StringUtils.isNull(resource)) {
            return;
        }else{
            resource.setPics(pics);
        }

        if(StringUtils.isNotNull(resource.getResourceId())){
            resourceMapper.update(resource);
        }else{
            resourceMapper.insert(resource);
        }
    }

    /**
     * 批量删除【资源
     *
     * @param resourceIds 需要删除的【资源ID
     * @return 结果
     */
    @Override
    public int deletByIds(Long[] resourceIds)
    {
        return resourceMapper.deleteByIds(resourceIds);
    }
}
